<template>
    <div v-show="activeSrc != ''" class="l-rblock" v-if="showSrcList.length > 0" >
      <iframe v-for="(src) in showSrcList" :key="src" v-show="activeSrc == src" :src="src" class="iframe" ref="iframe">
      </iframe>
    </div>
</template>

<script>
import { mapGetters } from "vuex";
export default {
  name:'l-page-iframe',
  data () {
    return {
      activeSrc:"",
      srcList:[],
    }
  },
  watch: {
    $route: {
      handler() {
        this.activeSrc = this.getSrc();
        if(this.activeSrc != "" && this.srcList.indexOf(this.activeSrc) == -1){
          this.srcList.push(this.activeSrc)
          this.srcList = this.srcList.filter(t=>this.tagSrcList.indexOf(t) != -1);
        }
      },
      immediate: true,
    },
  },
  computed: {
    ...mapGetters(["screen",'token','tagList',"modules"]),
    myModules() {
      if (this.loginInfo.f_SecurityLevel == 1) {
        return this.modules.filter((t) => t.f_EnabledMark == 1);
      }
      return this.modules.filter((item) => {
        if (item.f_EnabledMark == 1) {
          const moduleAuthIds = this.loginInfo.moduleAuthIds || [];
          if (moduleAuthIds.indexOf(item.f_ModuleId) > -1) {
            return true;
          } else {
            return false;
          }
        } else {
          return false;
        }
      });
    },
    tagSrcList() {
      return this.tagList
      .filter(
        (t) =>
          (this.myModules.findIndex(
            (t2) => t2.f_UrlAddress == t.value && t.label == t2.f_FullName
          ) != -1 || t.meta.isNotMenu) && this.isURL(t.value)
      ).map(t=>{
        let url = t.value.replace("$", "&")
        if(url.indexOf('lrtoken') != -1){
          url = url.replace("lrtoken", "token")
          url += this.token
        }
        return url
      });
    },
    showSrcList(){
      if(!this.appConfig.isTabKeepAlive || !this.lr_theme.showTabs){
        return [this.activeSrc]
      }
      return this.srcList.filter(t=>this.tagSrcList.indexOf(t) != -1)
    }
  },
  methods: {
    getSrc(){
      if(this.$route.query && this.$route.query.src && this.isURL(this.$route.query.src)){
        let url = this.$route.query.src.replace("$", "&")
        if(url.indexOf('lrtoken') != -1){
          url = url.replace("lrtoken", "token")
          url += this.token
        }
        return url
      }
      return ""
    },
    isURL (s) {
      if (s.includes('html')) return true;
      return /^http[s]?:\/\/.*/.test(s)
    }
  }
};
</script>

<style lang="scss">
.iframe {
  position: relative;
  width: 100%;
  height: 100%;
  z-index: 2;
  border: 0;
  overflow: hidden;
  box-sizing: border-box;
}
</style>